package com.lshl.lingshang.http.manager;

import android.text.TextUtils;

import com.lshl.lingshang.R;
import com.lshl.lingshang.application.MyApplication;
import com.lshl.lingshang.http.file.UploadFileRequestBody;
import com.lshl.lingshang.http.requestbean.BaseResponseBean;
import com.lshl.lingshang.utils.LogUtil;
import com.lshl.lingshang.utils.NetworkUtils;
import com.lshl.lingshang.utils.ResLoaderUtils;
import com.lshl.lingshang.utils.StringUtil;
import com.lshl.lingshang.utils.ToastUtil;

import java.io.IOException;
import java.net.ConnectException;
import java.net.UnknownHostException;

import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import retrofit2.HttpException;

/**
 * Description：封装loading
 * Created on 2018/6/28 0028
 * Author : 郭
 */
public abstract class SubscriberManger<T> implements Observer<BaseResponseBean<T>>, UploadFileRequestBody.OnProgressChangeCallBackListener {

    private static final String TAG = SubscriberManger.class.getSimpleName();

    private String message;

    public SubscriberManger() {
    }


    @Override
    public void onSubscribe(Disposable d) {
        if (!NetworkUtils.isNetworkConnected(MyApplication.getInstance().getApplicationContext())) {
            onError(new ConnectException());
        }

    }

    @Override
    public void onNext(BaseResponseBean<T> t) {
        //200  代表成功
        if (Integer.valueOf(t.getCode()) == 200) {
            try {
                onSuccess(t.getResult(), StringUtil.StrTrim(t.getMessage()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else { //服务器返回错误码
            if (!TextUtils.isEmpty(t.getMessage())) {
                try {
                    onFailure(StringUtil.StrTrimInt(t.getCode()), StringUtil.StrTrim(t.getMessage()));
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                ToastUtil.showToast(t.getMessage());
            } else {
                onError(new RuntimeException());
            }
        }
    }

    //成功回调
    public abstract void onSuccess(T result, String msg) throws IOException;

    //失败回调
    public abstract void onFailure(int code, String msg) throws IOException;

    //上传进度回调
    public void onProgress(int progress) {

    }

    @Override
    public void onError(Throwable e) {
        LogUtil.showLog(e.getMessage());
        if (e instanceof HttpException || e instanceof ConnectException || e instanceof UnknownHostException) {
            message = e.getMessage();
        } else {
            message = e.getMessage();
            if ("timeout".equals(message)) {
                message = ResLoaderUtils.getString(R.string.timeout_error);
            }
        }
        try {
            onFailure(-1, message);
        } catch (IOException e1) {
            e1.printStackTrace();
        }

    }

    @Override
    public void onComplete() {

    }

    //监听进度的改变
    @Override
    public void onProgressChangeCallBack(long bytesWritten, long contentLength) {
        onProgress((int) (bytesWritten * 100 / contentLength));
    }
}
